package src.edu.wayne.cs.csc4110.hw6.implementations.userPanels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import src.edu.wayne.cs.csc4110.hw6.implementations.VideoFrameImpl;
import src.edu.wayne.cs.csc4110.hw6.implementations.stateMachine.StateMachine;
import src.edu.wayne.cs.csc4110.hw6.interfaces.Game;



public class UserHomePanel extends StateMachinePanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1681547309611900119L;
	JLabel lblWelcome;
	
	public UserHomePanel(StateMachine stateMachine){
		super(stateMachine);
	
		//Create a panel
		this.setLayout(new BorderLayout(0, 175));
		this.setBackground(VideoFrameImpl.getColor());
		this.setForeground(VideoFrameImpl.getColor());
		lblWelcome = new JLabel("Welcome " + getStateMachine().getCurrentUser().getFullname());
		lblWelcome.setForeground(Color.WHITE);
		this.add(lblWelcome, BorderLayout.NORTH);
		
		
		//Put buttons on it
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,0));
		buttonPanel.setBackground(VideoFrameImpl.getColor());
		
		
		JPanel centerPanel = new JPanel();
		centerPanel.setBackground(VideoFrameImpl.getColor());
		ArrayList<Game> listOfGames = getStateMachine().getFrameWork().getGameList();
		if(listOfGames == null){
			centerPanel.setLayout(new GridLayout(0,1,2,2));
			centerPanel.add( new JTextField("You currently have no games to play."));
		}
		else{
			ArrayList<JButton> btnLst = getBtnLstFromGameLst(listOfGames);
			centerPanel.setLayout(new GridLayout(btnLst.size(),1,2,2));
			for(Iterator<JButton> gamesItr = btnLst.iterator(); gamesItr.hasNext();){
				centerPanel.add(gamesItr.next());
			}
			
		}	

		
		this.add(centerPanel, BorderLayout.CENTER);

		buttonPanel.add(getNextUserButton());
		buttonPanel.add(getAddUserButton());
		buttonPanel.add(getLogOutButton());
		
		this.add(buttonPanel, BorderLayout.SOUTH);
		
		String titleString = new String("Home Panel:  ");
		titleString = titleString.concat(stateMachine.getCurrentUser().getFullname());
		stateMachine.setDisplayPanel(this, titleString);
	}
	
	private JButton getNextUserButton()
	{
		JButton jbtnAddUser = new JButton("Switch To Next User");
		jbtnAddUser.addActionListener(
				new ActionListener() 
				{
					public void actionPerformed(ActionEvent e)
					{
						goToChangeToNextUserState();
					}
				}
			);
		return jbtnAddUser;
	}
	/**
	 * Creates the button used to log in other users into the system
	 * @author Kyle Andrews
	 * @author Daniel J. Borsodi
	 * @author Bryan Lawrence Taylor
	 * @return Fully prepared button to be clicked on to login another user
	 * */
	private JButton getAddUserButton()
	{
		JButton jbtnAddUser = new JButton("Login Another user");
		jbtnAddUser.addActionListener(
				new ActionListener() 
				{
					public void actionPerformed(ActionEvent e)
					{
						goToLoginState();
					}
				}
			);
		return jbtnAddUser;
	}
	/**
	 * Creates the log out button that is used on this screen. 
	 * This includes the action that is to be performed upon clicking on the button
	 * @author Kyle Andrews
	 * @author Daniel J. Borsodi
	 * @author Bryan Lawrence Taylor
	 * @return The built log out button
	 * */
	private JButton getLogOutButton()
	{
		JButton jbtnAddUser = new JButton("Log Current User Out");
		jbtnAddUser.addActionListener(
				new ActionListener() 
				{
					public void actionPerformed(ActionEvent e)
					{
						logOutCurrentUser();
					}
				}
			);
		return jbtnAddUser;
	}
	/**
	 * Logs the user in
	 * @author Kyle Andrews
	 * @author Daniel J. Borsodi
	 * @author Bryan Lawrence Taylor
	 * */
	public void goToLoginState(){
		stateMachine.setState(stateMachine.getNotLoggedInState());
		stateMachine.goToLoggedInState();
	}
	
	public void goToChangeToNextUserState(){
		stateMachine.setState(stateMachine.getChangeToNextUserState());
		stateMachine.goToLoggedInState();
	}
	/**
	 * Logs the user out.
	 * @author Kyle Andrews
	 * @author Daniel J. Borsodi
	 * @author Bryan Lawrence Taylor
	 * */
	public void logOutCurrentUser(){
		stateMachine.logOutCurrentUser();
	}
	private ArrayList<JButton> getBtnLstFromGameLst(ArrayList<Game> gameLst){
		ArrayList<JButton> gameBtns = new ArrayList<JButton>();
		for(Iterator<Game> gamesItr = gameLst.iterator(); gamesItr.hasNext();){
			final Game aGame = gamesItr.next();
			final JButton aGameBtn = new JButton(aGame.getGameName());
			aGameBtn.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					aGame.startGame();
					//JOptionPane.showMessageDialog(aGameBtn, "You clicked the button for " + aGameBtn.getText());

				}
			});
			gameBtns.add(aGameBtn);
		}
		return gameBtns;
	}

//	public void setPlayGameState(){
//		stateMachine.setState(stateMachine.getPlayGameState());
//		stateMachine.goToPlayGameState();
//	}
}
